package com.aeye.mbr.upms.rpc.service.impl;

import com.aeye.mbr.common.plugin.mapper.BaseServiceImpl;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.result.BootstrapTableResult;
import com.aeye.mbr.upms.common.constant.UpmsConstant;
import com.aeye.mbr.upms.dao.model.UpmsUser;
import com.aeye.mbr.upms.rpc.api.UpmsRoleService;
import com.aeye.mbr.upms.rpc.api.UpmsUserRoleService;
import com.aeye.mbr.upms.rpc.api.UpmsUserService;
import com.aeye.mbr.upms.rpc.mapper.UpmsUserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
* UpmsUserService实现
* Created by shuaeye.mbr on 2017/3/20.
*/
@Service
@Transactional
public class UpmsUserServiceImpl extends BaseServiceImpl<UpmsUser> implements UpmsUserService {

    private static Logger _log = LoggerFactory.getLogger(UpmsUserServiceImpl.class);

    @Autowired
    UpmsUserMapper upmsUserMapper;

    @Autowired
    UpmsUserRoleService upmsUserRoleService;

    @Autowired
    UpmsRoleService upmsRoleService;

    @Override
    public UpmsUser createUser(UpmsUser upmsUser) {
        Example upmsUserExample = new Example(UpmsUser.class);
        upmsUserExample.createCriteria()
                .andEqualTo("username",upmsUser.getUsername());
        long count = upmsUserMapper.selectCountByExample(upmsUserExample);
        if (count > 0) {
            return null;
        }
        upmsUserMapper.insertSelective(upmsUser);
        return upmsUser;
    }


    @Override
    public BootstrapTableResult<UpmsUser> selectUpmsUserByParams(String currentUser,BootstrapTableParams params) {
        BootstrapTableResult result = new BootstrapTableResult();
        if (params != null) {
            PageHelper.startPage(params.getPage(), params.getLimit());
            Example example = new Example(UpmsUser.class);
            String sort = params.getSort();
            String order = params.getOrder();
            String search = params.getSearch();
            if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
                example.setOrderByClause(sort + " " + order);
            }
            if(StringUtils.isNotBlank(currentUser) && !UpmsConstant.SUPER.equals(currentUser)){
                example.createCriteria().orEqualTo("createuser",currentUser).orEqualTo("username",currentUser);
            }
            if (StringUtils.isNotBlank(search)) {
                example.and().orLike("username","%" + search + "%").orLike("realname","%" + search + "%");
            }
            List<UpmsUser> users = upmsUserMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(users)) {
                if (users instanceof Page) {
                    Page<UpmsUser> pls = (Page) users;
                    result.setTotal((int) pls.getTotal());
                    result.setRows(pls.getResult());
                } else {
                    result.setTotal(users.size());
                    result.setRows(users);
                }
            }else{
                result.setTotal(0);
                result.setRows(users);
            }
        }
        return result;
    }


    public static void main(String[] args){

        for(int i = 0; i < 10; i++){
            int random = ((int)(Math.random()*100000000))%2;
            System.out.println(random);

        }
    }


	@Override
	public List<UpmsUser> selectUpmsUserByIdcard(String idcard, String name) {
		Example upmsUserExample = new Example(UpmsUser.class);
        upmsUserExample.createCriteria()
                .andEqualTo("relaname",name).andEqualTo("idcard", idcard);
        List<UpmsUser> users = upmsUserMapper.selectByExample(upmsUserExample);
        return users;
	}
}